package home

import (
	"myblog/app/model/module/category"
	newsModel "myblog/app/model/module/news"
	"myblog/app/utils/page"
)

/*
  List
  @Author: 皓云
  @Description:
  @param req
  @Date: 2021-09-01 10:47:59
  @return *category.EntityExtend
  @return []*newsModel.Entity
  @return *page.Paging
  @return error
  @Filepath:
*/
func List(req *newsModel.ListPageReq) (*category.EntityExtend, []*newsModel.Entity, *page.Paging, error) {
	//获取栏目相关信息
	cateinfo, err := category.SelectRecordById(req.Catid)
	if err != nil {
		return nil, nil, nil, err
	}
	//	通过catid 获取内容
	newslist, page, err := newsModel.ListByPage(req)
	if err != nil {
		return nil, nil, nil, err
	}
	return cateinfo, newslist, page, nil
}

/*
  Show
  @Author: 皓云
  @Description:
  @param newsid
  @Date: 2021-09-01 11:24:13
  @return *category.Entity
  @return *newsModel.ShowExtend
  @return error
  @Filepath:
*/
func Show(newsid int64) (*category.Entity, *newsModel.ShowExtend, error) {
	newsinfo := new(newsModel.ShowExtend)
	res, err := newsModel.FindOne("id=?", newsid)
	if err != nil {
		return nil, nil, err
	}
	newsinfo.Entity = res
	//获取栏目相关信息
	cateinfo, err := category.FindOne("id=?", newsinfo.Catid)
	if err != nil {
		return nil, nil, err
	}
	//上一页

	previous_page, err := newsModel.Model.Order("id DESC").FindOne("catid=? and id<?", cateinfo.Id, newsid)
	if err != nil {
		return nil, nil, err
	}
	newsinfo.PreviousPage = previous_page
	//下一页
	next_page, err := newsModel.FindOne("catid=? and id>?", cateinfo.Id, newsid)
	if err != nil {
		return nil, nil, err
	}
	newsinfo.NextPage = next_page
	return cateinfo, newsinfo, nil

}
/*
  AddViews
  @Author: 皓云
  @Description:
  @param newsid
  @param ip
  @Date: 2021-09-01 11:24:17
  @Filepath:
*/
func AddViews(newsid int64,ip string){
	newsModel.AddViews(newsid,ip)
}


